package com.company;

public class InstanceCounter {
    // 私有的静态类属性
    private static int numInstances = 0;
    // 受类保护的静态类方法
    protected static int getCount() {
        return numInstances;
    }
    // 类的私有静态方法
    private static void addInstance() {
        numInstances++;
    }
    //构造函数
    InstanceCounter() {
        InstanceCounter.addInstance();
    }

    public static void main(String[] arguments) {
        System.out.println("Starting with " +
                InstanceCounter.getCount() + " instances");
        for (int i = 0; i < 500; ++i){
            new InstanceCounter();
        }
        System.out.println("Created " +
                InstanceCounter.getCount() + " instances");
    }
}
